Repository navigation
Type min() / max() of two arguments as the union of both comparison orders - #7050
Conversation
|
//cc @SanderMuller |
|
as I read the PR description, before this PR we have unexpected errors for this cases:
if this is fixed now, we need tests for them |
|
Added the three cases as rule tests, |
do we also need to think about INF? |
|
PHP's behaviour across versions for the table above: https://3v4l.org/tVFpJ |
|
No, INF is covered. |
SanderMuller
left a comment
There was a problem hiding this comment.
I compared the types with real PHP 7.4 and 8.0 to 8.5 on 722 calls: min() and max() of every pair of 19 constants. The constants are ints, floats including -0.0, NAN, INF and -INF, numeric and non-numeric strings, bools and null. For each version I ran four forms: the direct call in the global namespace, \max() and max() in a namespace, and call_user_func(). Then I checked that every result is in the type for that phpVersion.
- On 2.3.x, 120 to 147 calls per version get a type that does not contain the real result. For example,
min(0, 0.0)is0.0where PHP returns0. With this PR it is 3 to 8. - On 7.4 to 8.3 the four forms give the same result for every call. On 8.4 and 8.5, 134 direct calls differ from
call_user_func(), and the three direct forms agree with each other. This supports your note that a version check could type 8.3 and older exactly.
Two cases still lose a value. Both are also wrong on 2.3.x:
max(NAN, -1)isNAN, but PHP 7.4 to 8.2 return-1, and so does a direct call on 8.4 and 8.5.max(-1, NAN)isNAN(*NEVER*on 2.3.x), but PHP 8.3 returns-1, and so doescall_user_func()on 8.4 and 8.5. With0in place of-1, both calls give0|NAN.max(-1, '')andmax('', -1)are*NEVER*withphpVersion8.0 to 8.5. PHP returns-1.
The comparisons themselves are typed correctly: NAN >= -1 ? NAN : -1 is -1, and -1 >= '' is true. So the value gets lost in the rewrite with AlwaysRememberedExpr, not in the comparison. I did not find the exact place.
The only other differences are min() of -0.0 with false or null, in either order. They give 0.0|false and 0.0|null where PHP can return -0.0. As -0.0 === 0.0, that only affects the description.
With a float $f, max(-1, $f) is -1|NAN|float<-0.9999999999999999, max> and max($f, -1) is -1|NAN|float<-1.0, max>. Both are correct.
minmax-nan.php and both testMinMaxNan() rule tests fail without the change in the extension and pass with it. On de379c9 the full test suite, make phpstan and phpcs pass. The five red CI jobs are also red on the base commit, or on #6539 and #7038.
Performance: the extra rewrite has a cost. I generated a file with 400 functions, each with seven min() / max() calls of two arguments. I analysed it at level 8 in two rounds of 8 runs per side.
- The median is 6.28 s on 2.3.x and 7.39 s with this PR.
- User CPU is 4.50 s against 5.53 s in the second round, and 4.65 s against 5.56 s in the first.
- That is about 0.4 ms of CPU for each of the 2,400 calls that do not have two integer arguments.
- The cost does not grow with nesting:
min()/max()nested 4 to 10 deep take 3.0 s on both sides.
|
Thanks for the matrix. Both remaining cases had one cause: the comparison narrowed an operand written as a constant expression such as |
|
Thanks. I ran the grid again on 7ce253f. Now every type contains the real result on 7.4 to 8.5, also for the Two things I found with variables:
I think the cause is the narrowing by a comparison with NAN, which also happens outside if (NAN >= $n) { /* $n is -2|-1 */ } else { /* $n is *NEVER* */ }
if ($n < NAN) { /* *NEVER* */ } else { /* *NEVER* */ }In PHP both comparisons are false for every The min/max nsrt tests and both comparison rule tests pass on 7ce253f. I did not run the full suite or the benchmark again. |
|
Good catch, that was my regression. I dropped I agree about the cause of the second one, and it is phpstan/phpstan#15713: in the false branch of a comparison, |
…rders MinMaxFunctionReturnTypeExtension typed min($a, $b) as $a < $b ? $a : $b and max($a, $b) as $a < $b ? $b : $a. Which argument PHP returns when the two compare as equal or one of them is NAN depends on the PHP version and on how the call was compiled. PHP 8.4 added a frameless handler for direct calls, which disagrees with the function body that call_user_func(), array_map() and unpacked calls use. A direct call also falls back to the function body under Xdebug and for unqualified nested calls inside a namespace. PHPStan cannot tell these apart. Since float ranges, the comparison in the rewrite narrows floats, so picking one order dropped values PHP returns: max(0.0, $float) was float<0.0, max> although max(0.0, NAN) is NAN on PHP 7.4 to 8.2 and for direct calls on 8.4+. Regardless of float ranges, min(0, 0.0) was 0.0 although it is 0 on PHP 8.3 and older. For two arguments that cannot be arrays, return the union of both rewrites: $a < $b ? $a : $b and $b < $a ? $b : $a for min(), and $a < $b ? $b : $a and $a >= $b ? $a : $b for max(). Integers are totally ordered and equal integers are the same value, so for two integers the first rewrite is enough.
… rules Kill the mutants of the integer shortcut in MinMaxFunctionReturnTypeExtension.
…n the rewrite The comparison with NAN narrowed a constant such as -1 to never in the branch that returns it.
Narrowing skips literals, so a comparison with NAN no longer turns one into never, and an argument whose type is a union of constants is narrowed again.
3997266 to
14e39d6
Compare
|
@SanderMuller any more input/edge-cases? :) |
|
One more. The
With a global Deciding by the type instead of the node fixes both for me: if (count($type->getConstantScalarValues()) === 1) {
return new TypeExpr($type);
}A single value cannot be narrowed anyway, and a union of constants still gets |
…node A constant that is not written as a literal, such as a class constant, went through AlwaysRememberedExpr and the comparison with NAN narrowed it to never.
|
Thank you! If there is still something left, please open new issues |
|
Thanks for all the verification on this one, and sorry for the late reply — it was merged as 94519e2 before I got back to it. I took the I fixed the remaining case in #7058: |
MinMaxFunctionReturnTypeExtensiontypesmin($a, $b)as$a < $b ? $a : $bandmax($a, $b)as$a < $b ? $b : $a. When the arguments compare as equal or one of them is NAN, which argument PHP returns depends on the PHP version and on how the call was compiled. This PR types both calls as the union of the two orders PHP uses.Impact
Since float ranges (#6937), the comparison in the rewrite narrows floats, so picking one order drops values that PHP can return.
$fisfloat)max(0.0, $f)float<0.0, max>NAN|float<0.0, max>min($f, 1.0)float<min, 1.0>NAN|float<min, 1.0>max(0.0, min(1.0, $f))float<0.0, 1.0>NAN|float<0.0, 1.0>max(1, NAN)11|NANmin(0, 0.0)0.00|0.0max(0, 0.0)00is_nan(max(0.0, $f))as always false. It is true for NAN on PHP 7.4 to 8.2, 8.4 and 8.5.is_nan(\max(0.0, \min(1.0, $f)))as always false. It is true for NAN for a direct call on PHP 8.4 and 8.5.\min($n, 100) === 100.0withint|float $nas always false. It is true for100.0on PHP 7.4 to 8.3.float<min, max>, or after!is_nan($f)) andmax()of equal values keep their current types.min()of a float and an integer that can be equal gains the other argument, likemin(0, 0.0).float. The rows with constants predate Add FloatRangeType for float<min, max> #6937.I found this while comparing #6937 with a
FloatRangeTypeprototype I had been working on before it landed. My own review of that prototype had flagged the samemax()rewrite.Cause
PHP has two implementations of
min()andmax()with two arguments: the function itself and, since PHP 8.4, a frameless handler. The function also changed in PHP 8.3. They agree unless the arguments compare as equal or cannot be ordered.max(0.0, NAN)NAN0.00.0NANmax(NAN, 0.0)0.0NANNAN0.0min(0.0, NAN)0.00.00.0NANmin(NAN, 0.0)NANNANNAN0.0min(0, 0.0)0000.0max(0, 0.0)0000Measured on 7.4.33, 8.0.30, 8.1.34, 8.2.35, 8.3.36, 8.4.27 and 8.5.12.
call_user_func('max', …),array_map('max', …)andmax(...$args), which this extension types too.developmode,\max(0.0, NAN)is0.0on PHP 8.5.12. Inside a namespace,max(0.0, min(1.0, NAN))without imports is1.0, while\max(0.0, \min(1.0, NAN))isNAN.min()is the one the frameless handler uses. The one ofmax()matches the function since PHP 8.3 for integers and floats. Neither holds for every call.min()/max()) covers the framelessmin()returning the second of two equal values. It does not cover NAN.Fix
For two arguments that cannot be arrays, the extension now returns the union of both rewrites:
$a < $b ? $a : $band$b < $a ? $b : $aformin(), and$a < $b ? $b : $aand$a >= $b ? $a : $bformax(). When both arguments are integers the two rewrites give the same values, so it evaluates only the first one.I did not make it depend on the PHP version. PHP 8.3 and older have one implementation per version, so a version check could type them exactly, and
max(0.0, min(1.0, $f))would stayfloat<0.0, 1.0>there.max()would need a different rewrite up to 8.2 and on 8.3. I can add that if you prefer it.Other sites
min()/max()with three or more arguments, a single array or an unpacked array go throughprocessType(). For scalar arguments it returns the union of the arguments unless they are constants that order strictly, so it does not drop NAN. This PR does not touch it.processType()still picks one side for constant arrays whose elements cannot be ordered.max([[NAN, 1], [0.0, 2]])is typedarray{0.0, 2}, and PHP 7.4 to 8.5 return[NAN, 1]. This is older than Add FloatRangeType for float<min, max> #6937 and not part of this PR.Testing
tests/PHPStan/Analyser/nsrt/minmax-nan.phpcovers NAN on either side, float ranges with and without NAN, equal values of different types, integer ranges,call_user_func()andarray_map(). Without the change, 26 of its assertions fail.minmax-nan.phpmake testsmake testswith the turbo extension loadedmake phpstanThe benchmarks in
tests/bench/datado not callmin()ormax().